package org.example.now;

import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.retry.annotation.Backoff;
import org.springframework.retry.annotation.EnableRetry;
import org.springframework.retry.annotation.Recover;
import org.springframework.retry.annotation.Retryable;

@SpringBootApplication
@EnableRetry
public class RetryApplication {

    public static void main(String[] args) {
        SpringApplication.run(RetryApplication.class,args);
        System.out.println("-----------------启动--------------------");
    }

    /**
     * 出现RuntimeException异常，再重试3次，每次延迟5s，之后每次延迟翻倍
     */
    @Retryable(include = {RuntimeException.class},maxAttempts = 4,backoff = @Backoff(delay = 2000L,multiplier = 2))
    public void work2(){
        System.out.println("执行-------------");
        throw new RuntimeException();
    }


    /**
     * 当重试次数达到限定时，会执行@Recover注解的补偿方法，只有在入参与发生异常匹配时才会执行该补偿方法
     * @param e
     */
    @Recover
    public void recover(RuntimeException e){
        System.out.println("------------执行补偿方法");
    }
}
